package com.jh.review.controller;

import com.jh.review.dao.TblFormMapper;
import com.jh.review.service.IHttpservice;
import com.jh.review.service.IReviewService;
import com.jh.review.util.JsonUtil;
import com.jh.review.util.ReadPropertiesUtils;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: guoc
 * @createDate: 2020/7/24 10:09
 */
@RestController
public class ReviewController {

    @Autowired
    IHttpservice httpservice;

    @Autowired
    TblFormMapper tblFormMapper;

    @Autowired
    IReviewService reviewService;

    /**
     * 直接读取配置文件，直接请求方法
     * @return java.lang.String
     * @author guoc
     * @date 2020/7/24 13:40
     */
    @RequestMapping("httpController")
    public String  httpController(){
        String url = ReadPropertiesUtils.getConfig("url");
        String paramStr = ReadPropertiesUtils.getConfig("param");
        String retnrnStr = httpservice.httpClientJsonTest(url, paramStr);
        if (StringUtils.isNoneBlank(retnrnStr)){
            //formid错误报500情况
            boolean jsonObject = JsonUtil.isJsonObject(retnrnStr);
            if (!jsonObject){
                return retnrnStr;
            }
            JSONObject returnJSON = JSONObject.fromObject(retnrnStr);
            int success = (int)returnJSON.get("success");
            if (success==0){
                //调用200，返回为seccess:0
                return (String) returnJSON.get("mes");
            }
            //成功
            return "SUCCESS";
        }
        //本地调用失败
        return "远程调用失败！";
    }

    /**
     * 查询所有手机表单接口
     * @return java.lang.String
     * @author guoc
     * @date 2020/7/24 13:41
     */
    @RequestMapping("getAllMobileFormIdController")
    public String  getAllMobileFormIdController(){
        List<String> allMobileFormId = tblFormMapper.getAllMobileFormId();
        if (allMobileFormId.size()>0){
            return allMobileFormId.toString();
        }
        return null;
    }

    /**
     * 手动请求验证接口
     * @return java.lang.String
     * @author guoc
     * @date 2020/7/24 13:41
     */
    @RequestMapping("reviewByMobileFormIdController")
    public String  reviewByMobileFormIdController(){
        List<String> allMobileFormId = reviewService.getAllMobileFormId();
//        List<String> allMobileFormId= new ArrayList<>();
//        allMobileFormId.add("2c91c822736fd4f801736ffc1e61138d");
        if (allMobileFormId!=null&& allMobileFormId.size()>0){
            Map<String,Object> returnMap=new HashMap<>();
            for (String formId : allMobileFormId) {
                String retnrnStr = reviewService.httpService(formId);
                if (StringUtils.isNoneBlank(retnrnStr)){
                    if ("SUCCESS".equals(retnrnStr)){
                        continue;
                    }
                    returnMap.put(formId,retnrnStr);
                }else {
                    returnMap.put(formId,"远程调用失败");
                }
            }
            if (returnMap.size()>0){
                Map<String, Object> stringObjectMap = reviewService.removeFormId(returnMap);
                return stringObjectMap.toString();
            }else {
                return "手机表单接口无异常";
            }
        }
        return "查询表单失败";
    }

}
